package com.item.controller;

import com.item.bean.*;
import com.item.dto.FlutterRoomItem;
import com.item.utils.QiNiuYunUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FlutterController {

    @Autowired
    QiNiuYunUtils qiNiuYunUtils;

    @GetMapping(value = "/api/get/data/list")
    public Map<String, String> getDataList() {

        Map<String, String> map = new HashMap<>();
        map.put("1", "林坑杰4两块");
        map.put("3", "林坑43462杰4两块");
        map.put("4", "林坑2326杰4两块");
        map.put("5", "林坑563杰4两块");

        return map;
    }

    /**
     * 注册业务
     *
     * @param flutterRoomItem
     * @return
     */
    @PostMapping(value = "/api/user/register")
    public FlutterBean getUserInfo(@RequestBody FlutterRoomItem flutterRoomItem) {
        System.out.println(flutterRoomItem);
        if (flutterRoomItem.getUsername() != null && flutterRoomItem.getPass() != null) {
            return new FlutterBean(true, "请求成功了", "1234234132412342");
        } else {
            return new FlutterBean(false, "请求失败", null);
        }


    }

    /**
     * 登录业务
     *
     * @param flutterRoomItem
     * @return
     */
    @PostMapping(value = "/api/user/login")
    public FlutterBean getUserLogin(@RequestBody FlutterRoomItem flutterRoomItem) {
        if (flutterRoomItem.getUsername() != null && flutterRoomItem.getPass() != null) {
            return new FlutterBean(true, "请求成功了", "666666666666666666666666");
        } else {
            return new FlutterBean(false, "请求失败", null);
        }
    }

    /**
     * 获取用户...
     *
     * @return
     */
    @GetMapping(value = "/api/get/user")
    public FlutterUserName getUserInfo() {
        FlutterUserName user = new FlutterUserName();
        user.setId(1231);
        user.setAvatar("http://r0utoyzux.hn-bkt.clouddn.com/155151.jpg");
        user.setGender("女");
        user.setNickname("美女");
        user.setPhone("1384388888");
        return user;
    }

    @GetMapping(value = "/houses/conditin/{cityId}")
    public FluterBody getFlutterBodyCityList(@PathVariable String cityId) {
        //返回的
        FluterBody fluterBody = new FluterBody();
        FlutterBodyList flutterBodyList = new FlutterBodyList();
        /**-----------------------------------------------------*/
        //区域
        List<GenerType> area = new ArrayList<>();
        area.add(new GenerType("区域1", "1"));
        area.add(new GenerType("区域2", "2"));
        area.add(new GenerType("区域3", "3"));
        area.add(new GenerType("区域4", "4"));
        //价格
        List<GenerType> priceList = new ArrayList<>();
        priceList.add(new GenerType("6000", "5"));
        priceList.add(new GenerType("8000", "6"));
        priceList.add(new GenerType("9000", "7"));
        priceList.add(new GenerType("10000", "8"));
        //出租类型
        List<GenerType> rentType = new ArrayList<>();
        rentType.add(new GenerType("出租类型1", "9"));
        rentType.add(new GenerType("出租类型2", "10"));
        rentType.add(new GenerType("出租类型3", "11"));
        rentType.add(new GenerType("出租类型4", "12"));
        //房屋类型
        List<GenerType> roomType = new ArrayList<>();
        roomType.add(new GenerType("房屋类型1", "13"));
        roomType.add(new GenerType("房屋类型2", "14"));
        roomType.add(new GenerType("房屋类型3", "15"));
        roomType.add(new GenerType("房屋类型4", "16"));
        //方向
        List<GenerType> oriented = new ArrayList<>();
        oriented.add(new GenerType("东南", "17"));
        oriented.add(new GenerType("西", "18"));
        oriented.add(new GenerType("北", "19"));
        oriented.add(new GenerType("南", "20"));
        //楼层
        List<GenerType> floor = new ArrayList<>();
        floor.add(new GenerType("高", "21"));
        floor.add(new GenerType("低", "22"));
        floor.add(new GenerType("中", "23"));
        floor.add(new GenerType("超高", "24"));


        /**-----------------------------------------------------*/
        flutterBodyList.setArea(area);
        flutterBodyList.setPriceList(priceList);
        flutterBodyList.setRentType(rentType);
        flutterBodyList.setRoomType(roomType);
        flutterBodyList.setOriented(oriented);
        flutterBodyList.setFloor(floor);
        fluterBody.setBody(flutterBodyList);
        return fluterBody;
    }

    @GetMapping(value = "/houses/list")
    public FlutterRoomListItemBody roomListItem(@Param("cityId") String cityId
            , @Param("area") String area
            , @Param("mode") String mode
            , @Param("price") String price
            , @Param("more") String more) {

        FlutterRoomListItemBody flutterRoomListItemBody = new FlutterRoomListItemBody();

        //System.out.println(cityId+area+mode+price+more);
        System.out.println("地区id---" + cityId);
        System.out.println("区域---" + area);
        System.out.println("出租类型---" + mode);
        System.out.println("租金---" + price);
        System.out.println("户型-朝向-楼层的ids---" + more);


        List<FlutterRoomListItem> list = new ArrayList<>();
        List<String> tags = new ArrayList<>();
        tags.add("近地铁");
        tags.add("集中供暖");
        tags.add("新上");
        tags.add("随时看房");
        //------------------------
        for (int i = 1; i <= 10; i++) {
            list.add(new FlutterRoomListItem(i + "",
                    "朝阳门南大街2室1厅8300元二室",
                    "二室/114/东 | 北 /朝阳门南大街",
                    "http://r0utoyzux.hn-bkt.clouddn.com/bo.jpg",
                    tags,
                    1200));
        }

        flutterRoomListItemBody.setBody(list);
        return flutterRoomListItemBody;
    }

//    @GetMapping(value = "/houses/list")
//    public List<FlutterRoomListItem> roomListItem(){
//        //System.out.println(object);
//        return null;
//    }

    /**
     * 房屋详情
     *
     * @param roomId
     * @return
     */
    @GetMapping(value = "/api/room/detail/{roomId}")
    public FlutterRoomDetailBody getRoomDetail(@PathVariable(value = "roomId") String roomId) {

        FlutterRoomDetailBody flutterRoomDetailBody = new FlutterRoomDetailBody();

        FlutterRoomDetail flutterRoomDetail = new FlutterRoomDetail();
        flutterRoomDetail.setId("1111");
        flutterRoomDetail.setTitle("整租 中山路 历史最低2块钱");
        flutterRoomDetail.setCommunity("中山花园");
        flutterRoomDetail.setSubTitle("近地铁 附近有商场253 253452 34523 5234 5234 5234 5234 5234 5234 5234 5234 52345 234 5234 52地铁 附近有商场253 253452 34523 5234 5234 5234 5234 5234 5234 5234 5234 52345 234 5地铁 附近有商场253 253452 34523 5234 5234 5234 5234 5234 5234 5234 5234 52345 234 5地铁 附近有商场253 253452 34523 5234 5234 5234 5234 5234 5234 5234 5234 52345 234 5地铁 附近有商场253 253452 34523 5234 5234 5234 5234 5234 5234 5234 5234 52345 234 5地铁 附近有商场253 253452 34523 5234 5234 5234 5234 5234 5234 5234 5234 52345 234 54 ");
        flutterRoomDetail.setSize(100);
        flutterRoomDetail.setFloor("高楼层");
        flutterRoomDetail.setPrice(3000);
        flutterRoomDetail.setRenovation("精装");
        List<String> oriented = new ArrayList<>();
        oriented.add("东南");
        flutterRoomDetail.setOriented(oriented);
        flutterRoomDetail.setRoomType("三室");
        List<String> applicances = new ArrayList<>();
        applicances.add("衣柜");
        applicances.add("洗衣机");
        applicances.add("电视");
        applicances.add("宽带");
        applicances.add("沙发");
        applicances.add("天然气");

        flutterRoomDetail.setApplicances(applicances);
        List<String> tags = new ArrayList<>();
        tags.add("近地铁");
        tags.add("集中供暖");
        tags.add("新上");
        tags.add("随时看房");

        flutterRoomDetail.setTags(tags);
        List<String> houseImgs = new ArrayList<>();
        houseImgs.add("http://r0utoyzux.hn-bkt.clouddn.com/bo.jpg");
        houseImgs.add("http://r0utoyzux.hn-bkt.clouddn.com/bo.jpg");
        houseImgs.add("http://r0utoyzux.hn-bkt.clouddn.com/bo.jpg");
        houseImgs.add("http://r0utoyzux.hn-bkt.clouddn.com/bo.jpg");
        flutterRoomDetail.setHouseImgs(houseImgs);
        flutterRoomDetailBody.setBody(flutterRoomDetail);
        return flutterRoomDetailBody;
    }

    /**
     * 获取小区
     *
     * @param cityId
     * @return
     */
    @GetMapping(value = "/hource/community/{value}/{cityId}")
    public FlutterGenerType getCommunityList(@PathVariable(value = "value") String value, @PathVariable(value = "cityId") String cityId) {
        FlutterGenerType flutterGenerType = new FlutterGenerType();
        List<FlutterGenerTypeItem> generTypes = new ArrayList<>();
        generTypes.add(new FlutterGenerTypeItem("1", "小区1"));
        generTypes.add(new FlutterGenerTypeItem("2", "小区2"));
        generTypes.add(new FlutterGenerTypeItem("3", "小区3"));
        flutterGenerType.setBody(generTypes);
        return flutterGenerType;
    }

    @RequestMapping(value = "/houses/params")
    public FlutterGetListBody getHousesParams() {
        FlutterGetListBody flutterGetListBody = new FlutterGetListBody();
        GetListBody getListBody = new GetListBody();

        List<GenerType> floor = new ArrayList<>();
        floor.add(new GenerType("1", "1室"));
        floor.add(new GenerType("2", "2室"));
        floor.add(new GenerType("3", "3室"));

        List<GenerType> oriented = new ArrayList<>();
        oriented.add(new GenerType("1", "低层"));
        oriented.add(new GenerType("2", "中层"));
        oriented.add(new GenerType("3", "高层"));

        List<GenerType> roomType = new ArrayList<>();
        roomType.add(new GenerType("1", "东南"));
        roomType.add(new GenerType("2", "南"));
        roomType.add(new GenerType("3", "西"));


        getListBody.setFloor(floor);
        getListBody.setOriented(oriented);
        getListBody.setRoomType(roomType);
        flutterGetListBody.setBody(getListBody);
        return flutterGetListBody;
    }

    /**
     * 房屋添加
     */
    @PostMapping(value = "/room/add")
    public String roomAdd(@RequestBody Map<String, Object> map) {
        System.out.println(map);
        return "";
    }

    /**
     * 文件上传
     */
    @RequestMapping("/flutter/uploadPictures") //uploadPictures 这里的接口
    public String uploadPictures(@RequestParam("files") MultipartFile[] files) {

        //System.out.println(files);
        System.out.println("进来了");
        String url = "";
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                try {

                    //System.out.println(files);
                    FileInputStream fileInputStream = (FileInputStream) files[i].getInputStream();

                    url = qiNiuYunUtils.uploadQiniuYun(fileInputStream);

                    //System.out.println(url);
                    //输出url上传后的,可以复制url到浏览器访问
                    //System.out.println("url=" + url);
                    try {
                        //延迟两秒让七牛云缓一下 2000 等于2秒 不能缓的时间太长,前端没有渲染到图片会报错
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("返回图片地址" + url);
        //上传图片成功
        return url;
    }


}
